package com.good.daoImp;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.good.dao.RoleDao;
import com.good.entity.Abs;
import com.good.entity.Role;
@Repository("roleDaoImpl")
public class RoleDaoImpl extends BaseDaoImpl<Role,Long> implements RoleDao {

	@Override
	public Role findByName(String name) {
		Query  query=this.getSession().createQuery("from Role where name=:name");
		if(name!=null){
			query.setParameter("name", name);
			try{
				return (Role) query.uniqueResult();
			}catch(Exception e){
				System.out.println(e);
				return null;
			}
		}
		return null;		
	}

	@Override
	public List<Role> findByLikeName(String name) {
		Query query=this.getSession().createQuery("from Role where name like :name");
		if(name!=null){
			query.setParameter("name", "%"+name+"%");
			return (List<Role>)query.list();
		}
		return null;
	}

	@Override
	public String findByRoleId(Long id) {
		Query query=this.getSession().createQuery("select role.name from Role role where id=:id");
		if(id!=null){
			query.setParameter("id", id);
			try{
				return (String) query.uniqueResult();
			}catch(Exception e){
				return null;
			}
		}
		return null;
	}

	@Override
	public Abs findById(Long id) {
		Query query=this.getSession().createQuery("from Abs where id=:id");
		if(id!=null){
			query.setParameter("id", id);
			return (Abs) query.uniqueResult();
		}
		return null;
	}

}
